using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Common;
using Common.Enum;
using Controls;
using ProjectView.Windows;
using Report;
using SProject;
using SProject.Paramter;
using SProject.Program;

namespace ProjectView.ProjectControls
{
	public partial class UCMeltingOption : System.Windows.Controls.UserControl
	{
		public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(UCMeltingOption), new PropertyMetadata(false));

		public static readonly DependencyProperty ProjectProperty = DependencyProperty.Register("Project", typeof(Project), typeof(UCMeltingOption), new UIPropertyMetadata(null));

		public bool IsReadOnly
		{
			get
			{
				return (bool)GetValue(IsReadOnlyProperty);
			}
			set
			{
				SetValue(IsReadOnlyProperty, value);
			}
		}

		public Project Project
		{
			get
			{
				return (Project)GetValue(ProjectProperty);
			}
			set
			{
				SetValue(ProjectProperty, value);
			}
		}

		public UCMeltingOption()
		{
			InitializeComponent();
			grid.addMenu(new Separator());
			System.Windows.Controls.MenuItem menuItem = new System.Windows.Controls.MenuItem();
			menuItem.Header = Utility.GetMessage("Project_Memu_Data_Export");
			menuItem.Click += MenuExportData_Click;
			grid.IsLineCopy_Paste_Visable = true;
			grid.IsContextMenuVisible = true;
			grid.addMenu(menuItem);
			AddHandler(DataGridEx.OnCopyRowRoutedEvent, new RoutedEventHandler(OnCopyRowRouted_RoutedEvent), true);
			if (ConfigReader.GetInstance().GetPeekTypeVisibleFromIni() == 1)
			{
				colPeektype.Visibility = Visibility.Visible;
			}
			if (ConfigReader.GetInstance().GetMeltParamVisibleFromIni() == 1)
			{
				colNoiseThreshold.Visibility = Visibility.Visible;
				colPeekWidthThreshold.Visibility = Visibility.Visible;
			}
		}

		private void OnCopyRowRouted_RoutedEvent(object sender, RoutedEventArgs e)
		{
			CopyRowRoutedEventArgs copyRowRoutedEventArgs = e as CopyRowRoutedEventArgs;
			if (copyRowRoutedEventArgs != null)
			{
				MeltingOptionItem meltingOptionItem = copyRowRoutedEventArgs.SourceItem as MeltingOptionItem;
				MeltingOptionItem meltingOptionItem2 = copyRowRoutedEventArgs.TargetItem as MeltingOptionItem;
				if (meltingOptionItem != null && meltingOptionItem2 != null)
				{
					meltingOptionItem2.MinTemperature = meltingOptionItem.MinTemperature;
					meltingOptionItem2.MaxTemperature = meltingOptionItem.MaxTemperature;
					meltingOptionItem2.NoiseThreshold = meltingOptionItem.NoiseThreshold;
					meltingOptionItem2.PeekHightThreshold = meltingOptionItem.PeekHightThreshold;
					meltingOptionItem2.PeekWidthThreshold = meltingOptionItem.PeekWidthThreshold;
					meltingOptionItem2.PeekType = meltingOptionItem.PeekType;
					e.Handled = true;
				}
			}
		}

		private void Root_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			RefreshColumn();
		}

		private void RefreshColumn()
		{
			BasicOption basicOption = base.DataContext as BasicOption;
			if (basicOption != null)
			{
				basicOption.Items.CollectionChanged += Items_CollectionChanged;
				RefreshColumnTubeNameVisibility();
			}
		}

		private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			RefreshColumnTubeNameVisibility();
		}

		private void RefreshColumnTubeNameVisibility()
		{
			BasicOption basicOption = base.DataContext as BasicOption;
			if (basicOption != null)
			{
				if (basicOption.Items.Select((BasicOptionItem s) => s.TubeNo).Distinct().Count() <= 1 || basicOption.Items.Where((BasicOptionItem s) => !string.IsNullOrEmpty(s.TubeName)).Count() == 0)
				{
					columnTubeName.Visibility = Visibility.Collapsed;
				}
				else
				{
					columnTubeName.Visibility = Visibility.Visible;
				}
			}
		}

		public bool Validate()
		{
			bool flag = true;
			MeltingOption meltingOption = base.DataContext as MeltingOption;
			if (meltingOption != null && Project != null)
			{
				grid.CommitEdit();
				double num = (from s in Project.PCRProgram.Programs
					where s is MeltingSegment
					select ((MeltingSegment)s).StartTemp).FirstOrDefault();
				double num2 = (from s in Project.PCRProgram.Programs
					where s is MeltingSegment
					select ((MeltingSegment)s).EndTemp).FirstOrDefault();
				int index = -1;
				int index2 = -1;
				StringBuilder stringBuilder = new StringBuilder();
				for (int i = 0; i < meltingOption.Items.Count; i++)
				{
					MeltingOptionItem meltingOptionItem = meltingOption.Items[i] as MeltingOptionItem;
					index2 = i;
					string text = Utility.GetMessage("Target") + "[" + meltingOptionItem.TargetName + "]:";
					if (meltingOptionItem.MinTemperature < num)
					{
						stringBuilder.AppendFormat(text + Utility.GetMessage("msg_melt_paramter_mintemp_invalid"));
						index = 2;
						flag = false;
						break;
					}
					if (meltingOptionItem.MaxTemperature > num2)
					{
						stringBuilder.AppendFormat(text + Utility.GetMessage("msg_melt_paramter_maxtemp_invalid"));
						index = 3;
						flag = false;
						break;
					}
					if (meltingOptionItem.MinTemperature > meltingOptionItem.MaxTemperature)
					{
						stringBuilder.AppendFormat(text + Utility.GetMessage("msg_melt_paramter_mintemp_more_maxtemp"));
						index = 2;
						flag = false;
						break;
					}
				}
				if (!flag && stringBuilder.Length > 0)
				{
					grid.UnselectAllCells();
					System.Windows.MessageBox.Show(stringBuilder.ToString());
					grid.CurrentColumn = grid.Columns[index];
					grid.CurrentItem = meltingOption.Items[index2];
					grid.BeginEdit();
				}
			}
			return flag;
		}

		private void MenuExportData_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Title = Utility.GetMessage("Save");
			saveFileDialog.Filter = string.Format("{1}{0}|*.{2}|{3}{0}|*.{3}|{4}{0}|*.{4}", Utility.GetMessage("File"), "Excel", "xlsx", "csv", "txt");
			saveFileDialog.FilterIndex = 1;
			saveFileDialog.InitialDirectory = Utility.GetMyDocumentFolder();
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				base.Cursor = System.Windows.Input.Cursors.Wait;
				switch (saveFileDialog.FilterIndex)
				{
				case 1:
					SaveToExcel(saveFileDialog.FileName);
					break;
				case 2:
				case 3:
					SaveToCsv(saveFileDialog.FileName);
					break;
				}
				base.Cursor = System.Windows.Input.Cursors.Arrow;
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_data_export_sussess"));
			}
		}

		public void SaveToExcel(string file)
		{
			ExcelData excelData = new ExcelData();
			ExcelSheetData excelSheetData = new ExcelSheetData();
			excelSheetData.Name = Utility.GetMessage("melting_analysis_parameter");
			excelSheetData.Data = GetData(true);
			excelData.SheetDataList.Add(excelSheetData);
			ExcelOperator.SaveToExcel(file, excelData);
		}

		public void SaveToCsv(string file)
		{
			CsvTxtOperator.SaveToCsv(file, GetData());
		}

		private List<List<string>> GetData(bool includeMergerCellInfo = false)
		{
			List<List<string>> list = new List<List<string>>();
			List<string> list2 = new List<string>();
			foreach (DataGridColumn column in grid.Columns)
			{
				if (column.Visibility == Visibility.Visible)
				{
					list2.Add(column.Header.ToString());
				}
			}
			list.Add(list2);
			List<string> item = new List<string>();
			foreach (MeltingOptionItem item2 in (IEnumerable)grid.Items)
			{
				list2 = new List<string>();
				list2.Add(item2.TargetName);
				list2.Add(item2.ChannelNo.ToString());
				list2.Add(item2.MinTemperature.ToString("0.00"));
				list2.Add(item2.MaxTemperature.ToString("0.00"));
				list2.Add(item2.PeekHightThreshold.ToString("0.00"));
				if (colPeektype.Visibility == Visibility.Visible)
				{
					list2.Add((from s in EnumManagement.GetAllPeekType()
						where (EPeekType)s.Key == item2.PeekType
						select s.Value).FirstOrDefault() as string);
				}
				if (colNoiseThreshold.Visibility == Visibility.Visible)
				{
					list2.Add(item2.NoiseThreshold.ToString("0.00"));
					list2.Add(item2.PeekWidthThreshold.ToString("0.00"));
				}
				list.Add(list2);
			}
			if (includeMergerCellInfo)
			{
				list.Insert(0, item);
			}
			return list;
		}

		public void UnSelectAll()
		{
			grid.UnselectAllCells();
		}

		private void grid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			if (IsReadOnly)
			{
				e.Cancel = true;
			}
		}

		private void Command_Gain_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void Command_Gain_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			ProjectGainInfoWindow projectGainInfoWindow = new ProjectGainInfoWindow();
			projectGainInfoWindow.setData((base.DataContext as BasicOption).Items, EProjectType.Melt);
			projectGainInfoWindow.Owner = Utility.GetMainWindow();
			projectGainInfoWindow.ShowDialog();
		}
	}
}
